package com.liyou.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liyou.auth.service.SysMenuService;
import com.liyou.auth.service.SysUserService;
import com.liyou.common.config.exception.CustomException;
import com.liyou.common.jwt.JwtHelper;
import com.liyou.common.result.Result;
import com.liyou.common.utils.MD5;
import com.liyou.model.system.SysUser;
import com.liyou.vo.system.LoginVo;
import com.liyou.vo.system.RouterVo;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "后台登陆管理")
@CrossOrigin
@RestController
@RequestMapping("/admin/system/index")
public class IndexController {

    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysMenuService sysMenuService;

    //登录
    @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo) {
        /*Map<String, Object> map = new HashMap<>();
        map.put("token", "admin-token");
        return Result.ok(map);*/

        //1 获取输入的用户名
        String username = loginVo.getUsername();
        //2 根据用户名查询数据库
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername, username);
        SysUser sysUser = sysUserService.getOne(wrapper);
        //3 判断用户是否存在
        if (sysUser == null) {
            throw new CustomException(201, "用户不存在");
        }
        //4 判断用户名对应的密码是否正确
        //对输入的密码进行加密
        String inputPassword = loginVo.getPassword();
        String encrypt = MD5.encrypt(inputPassword);

        String dBPassword = sysUser.getPassword();
        //加密后的密码与数据库进行比对
        if (!encrypt.equals(dBPassword)) {
            throw new CustomException(201, "用户名或密码错误");
        }
        //5 判断用户是否被禁用
        if (sysUser.getStatus().intValue() == 0) {
            throw new CustomException(201, "用户已被禁用");
        }
        //6 使用jwt根据用户id和用户名称生成token字符串
        String token = JwtHelper.createToken(sysUser.getId(), sysUser.getUsername());
        //7 返回
        HashMap<String, Object> map = new HashMap<>();
        map.put("token", token);
        return Result.ok(map);
    }

    //
    @GetMapping("info")
    public Result info(HttpServletRequest request) {
        //1 从请求头获取用户信息
        String token = request.getHeader("token");
        //2 从token中获取用户id
        Long userId = JwtHelper.getUserId(token);
        //3 根据用户id查询用户信息
        SysUser sysUser = sysUserService.getById(userId);
        //4 根据用户id获取用户可以操作的菜单列表
        List<RouterVo> routerList = sysMenuService.findUserMenuListByUserId(userId);
        //5 根据用户id获取用户可以操作的按钮列表
        List<String> permsList = sysMenuService.findUserPermsByUserId(userId);

        Map<String, Object> map = new HashMap<>();
        map.put("roles", "[admin]");
        map.put("name", sysUser.getName());
        map.put("avatar", "https://oss.aliyuncs.com/aliyun_id_photo_bucket/defalt_handsome.jpg");
        map.put("routers", routerList);
        map.put("buttons", permsList);
        return Result.ok(map);
    }

    @PostMapping("logout")
    public Result logout() {
        return Result.ok();
    }

}
